home *** CD-ROM | disk | FTP | other *** search
- package sub_arctic.lib;
-
-
- import sub_arctic.input.*;
- import sub_arctic.output.*;
-
- /**
- * This is a "style-free" vertical scrollbar interactor. It works with
- * the style_manager and a style object.
- *
- * @author Ian Smith
- */
-
- public class v_scrollbar extends v_slider {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Indicate which values (coordinates/sizes) of this object are
- * intrinsically constrained by the internals of the object. In this case
- * we need to remove PART_B from the set provided by the superclass.
- *
- * @return int bitset indicating which parts are intrinsically constrained.
- */
- public int intrinsic_constraints()
- {
- return super.intrinsic_constraints() & ~ PART_B;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Compute the offset to the thumb in pixels. We override here so we never
- * return -1 for the doesn't fit condition (which was for use by sliders
- * that have a fixed size thumb), and just return 0 instead.
- *
- * @return int the amount the thumb should be shifted down to account for the
- * value of the slider.
- */
- protected int thumb_offset()
- {
- int result = super.thumb_offset();
- if (result < 0) result = 0;
- return result;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This holds the usable area of the scrollbar.
- */
- protected int _usable_area;
-
- /**
- * This computes the usable area of the scrollbar.
- */
- protected void compute_usable_area() {
- loaded_image up=up_img();
- loaded_image down=dn_img();
-
- /* sanity check */
- if ((up==null) || (down==null)) {
- _usable_area=0;
- }
-
- /* normal case */
- _usable_area=h()-(up.height()+down.height());
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Return the value of the part_b component of this object. In our case
- * this is tied to _thumb_percentage. In particular its
- * thumb_percentage*thumb_scale where thumb_percentage is in the range
- * 0..1.
- *
- * @return int the value of part_b (AKA thumb_percentage).
- */
- public int part_b()
- {
- /* Make sure its up to date and then return it */
- eval_part_b();
- return (int)(((double)_thumb_percentage)*thumb_scale() + 0.5);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Set part_b value directly bypassing the constraint system (but doing
- * damage). In this case part_b is tied to _thumb_percentage.
- *
- * @param int v the new value.
- */
- protected void set_raw_part_b(int v)
- {
- double new_v;
-
- /* compute the floating point equivalent */
- new_v = ((double)v)/thumb_scale();
- if (new_v < 0.0) new_v = 0.0;
- if (new_v > 1.0) new_v = 1.0;
-
- /* don't do anything unless this is a change */
- if (new_v != _thumb_percentage)
- {
- /* make change */
- _thumb_percentage = new_v;
-
- /* make sure we have the usable area and make a new image */
- compute_usable_area();
- calculate_thumb_image(); // this does damage_self();
- }
- }
-
- //had:
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Set the part_b component of this object. In our case this is tied to
- * thumb_percentage.
- *
- * @param int v the new value
- */
- public void set_part_b(int v)
- {
- double old_v;
-
- /* if this has a constraint throw an exception */
- if ((active_constraints() & PART_B) != 0)
- throw new sub_arctic_error(
- "Attempt to assign value to constrained part_b " +
- "(AKA v_scrollbar.thumb_percentage)");
-
- /* set the value */
- old_v = _thumb_percentage;
- set_raw_part_b(v);
-
- /* if its changed, mark it out of date */
- if (old_v != _thumb_percentage) mark_part_b_ood();
- }
-
- //had:
- //* @exception cannot_assign if the value is constrained.
- //* @exception PROPAGATED.
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * The amount we scale thumb_percentage (which is in the range 0..1) by when
- * return it (or accept it) as an integer through the part_b interface.
- * This defaults to 100.0 which corresponds to integer values being one
- * percent each (i.e., 0..100 is the legal range).
- */
- protected double _thumb_scale = 100.0;
-
- /**
- * The amount we scale thumb_percentage (which is in the range 0..1) by when
- * return it (or accept it) as an integer through the part_b interface.
- * This defaults to 100.0 which corresponds to integer values being one
- * percent each (i.e., 0..100 is the legal range).
- *
- * @return double the thumb scale value.
- */
- public double thumb_scale() {return _thumb_scale; }
-
- /**
- * The amount we scale thumb_percentage (which is in the range 0..1) by when
- * return it (or accept it) as an integer through the part_b interface.
- * This defaults to 100.0 which corresponds to integer values being one
- * percent each (i.e., 0..100 is the legal range). If you want more
- * resolution than this, set the scale to a larger number. Values <= 0
- * will restore the scale value to the default (100.0).
- *
- * @param double v the new thumb scale value.
- */
- public void set_thumb_scale(double v)
- {
- if (v <= 0) v = 100.0;
- if (v != _thumb_scale)
- {
- _thumb_scale = v;
- mark_part_b_ood();
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This is the amount as a percentage (0..1) of size the thumb takes up.
- */
- protected double _thumb_percentage;
-
- /**
- * Retrieve the amount of space covered by the thumb. This value is
- * tied to part_b of this object (which is accessible as a scaled integer
- * via the constraint system).
- *
- * @return double the size of the scrollbar as a percent (0..1) of the area.
- */
- public double thumb_percentage()
- {
- /* Make sure its up to date and then return it */
- eval_part_b();
- return _thumb_percentage;
- };
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Size of current thumb (so we know if we need to replace it) */
- protected int _cur_thumb_size = -1;
-
- /**
- * This does the work of actually drawing the thumb image.
- */
- public void calculate_thumb_image() {
- int pixel_width;
- int min=style_manager.current_style().v_scrollbar_minimum_thumb_size();
-
- /* what's the percentage */
- pixel_width=Math.round((float)(((double)_usable_area)*
- thumb_percentage()));
- /* is it too small? */
- if (pixel_width<min) {
- pixel_width=min;
- }
-
- /* if we have changed, build the thumb image */
- if (pixel_width != _cur_thumb_size)
- {
- set_thumb_img(style_manager.current_style().
- v_scrollbar_thumb(pixel_width));
- _cur_thumb_size = pixel_width;
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Set the percentage of the whole size the thumb is now occupying.
- *
- * @param double the percentage of the size of the scrollbar the thumb is
- * occupying (0.0 < this number <= 1.0)
- */
- public void set_thumb_percentage(double d) {
-
- /* force d in range */
- if (d < 0.0) d = 0.0;
- if (d > 1.0) d = 1.0;
-
- /* only do something if we have a change */
- if (d==_thumb_percentage) return;
-
- /* do the assignment and mark part_b out of date as a result */
- _thumb_percentage = d;
- mark_part_b_ood();
-
- /* make sure we have the usable area and recompute image */
- compute_usable_area();
- calculate_thumb_image(); // this does damage_self();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Internal function for initializing the scrollbar.
- */
- protected void init() {
-
- /* this is the same as a change */
- style_changed();
-
- /* set our intrinsic width to the width of any of the images */
- /* sanity check first */
- if (up_img()==null) {
- set_intrinsic_w(10); // bogus but the user will want to see something
- } else {
- set_intrinsic_w(up_img().width());
- }
-
- set_boxed(false); /* do we never want this? */
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Create a vertical scrollbar at a given location on the screen with
- * a given height.
- * @param int x the x position of this object.
- * @param int y the y position of this object.
- * @param int h the height of this object.
- * @param int min the min value of this object.
- * @param int max the max value of this object.
- * @param int init the initial value of this object.
- * @param double percent the size of the scrollbar (larger than
- * zero, less than or equal to one).
- * @param int small the small increment value (which occurs
- * when you press a end button).
- * @param int large the large increment value (which occurs
- * when you click in the thumb area).
- * @param callback_object co the object to send callbacks to (#0 is the
- * mouse has been released and the thumb is
- * stopped moving and #1 is the thumb is being
- * dragged).
- */
- public v_scrollbar(int x, int y, int h, int min, int max, int init,
- double percent,int small, int large,
- callback_object co) {
-
- // temporarily construct a default vertical slider
- super(x,y,h,min,max,init,small,large,co);
-
- /* put the percent in */
- if (percent<0.0) {
- _thumb_percentage=0.0;
- } else if (percent>=1.0) {
- _thumb_percentage=1.0;
- } else {
- _thumb_percentage=percent;
- }
-
- init();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Create a vertical scrollbar with some reasonable defaults.
- * This makes the range of the scrollbar 0-99 and sets the initial
- * value at 0. It makes the small increment 5 and the large
- * increment 25. It sets the thumb to be 25% of the size of the
- * scrollbar
- *
- * @param int x the x position of the interactor.
- * @param int y the x position of the interactor.
- * @param int w the width of the interactor.
- * @param callback_object co the object to send callbacks to.
- */
- public v_scrollbar(int x, int y, int w, callback_object co ) {
-
- super(x,y,w,0,99,0,5,25,co);
-
- /* set the percentage */
- _thumb_percentage=0.25;
-
- /* set things up */
- init();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Minimal constructor for a vertical scrollbar. It assumes
- * you are going to size it and position it with constraints
- * or do so directly.<p>
- *
- * This makes the range of the slider 0-99 and sets the initial
- * value at 0. It makes the small increment 5 and the large
- * increment 25. It sets the thumb to be 25% of the size of the
- * scrollbar
- *
- * @param callback_object co the object to send callbacks to
- */
- public v_scrollbar(callback_object co ) {
-
- super(0,0,50,0,99,0,5,25,co);
-
- /* set the percentage */
- _thumb_percentage=0.25;
-
- /* set things up */
- init();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This function gets called when the system's style resources
- * change or we are getting initialized.
- */
- public void style_changed() {
- style cs=style_manager.current_style();
- loaded_image img[];
-
- /* make the images */
- img=cs.v_scrollbar_images();
-
- /* set them into their places */
- set_up_img(img[0]);
- set_dn_img(img[1]);
- set_back_img(img[2]);
-
- /* recalculate the thumb */
- compute_usable_area();
- calculate_thumb_image();
-
- /* put the shift on.. */
- set_thumb_shift(cs.v_scrollbar_thumb_shift());
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * We override configure so we can automatically update the
- * size of the scrollbar thumb.
- */
- public void configure() {
- int pixel_height;
- int min=style_manager.current_style().v_scrollbar_minimum_thumb_size();
-
- /* if things start breaking, this might need to go */
- super.configure();
-
- /* make sure we have the usable area right */
- compute_usable_area();
-
- /**
- * WARNING: This code is basically the same as the code in
- * calculate_thumb_image() but it cannot call anything which
- * would result in a call to damage_self() since this
- * function is called in response to that.
- */
- /* what's the percentage */
- pixel_height=Math.round((float)(((double)_usable_area)*
- thumb_percentage()));
- /* is it too small? */
- if (pixel_height<min) {
- pixel_height=min;
- }
-
- /* if we have a change, build the thumb image */
- if (pixel_height != _cur_thumb_size)
- {
- _thumb_img = style_manager.current_style().
- v_scrollbar_thumb(pixel_height);
- _cur_thumb_size = pixel_height;
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
-
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-